package web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;

import com.CreateRadomTime;
import com.DBCon;

/**
 * Servlet implementation class ServletCreateCheckData
 */
@WebServlet({ "/ServletCreateCheckData", "/Manager/CheckData" })
public class ServletCreateCheckData extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//生成User表测试集
		String sql;
		String[][] UserNum=new String[50][2]; //50个教师账号
		String sql_findDepName;
		
		String[] StringDatePart1=new String[5];
		
		ResultSet rs;
		Random rd=new Random();
		DBCon DC=new DBCon();
		
		CreateRadomTime CR=new CreateRadomTime();//生成一个可以生成随机时间的对象
	
		SimpleDateFormat Dateformat = new SimpleDateFormat("yyyy-MM-dd");  
		//设置Date类型的日期年月日
		for(int i=0;i<5;i++) {
			Date date=new Date();
			GregorianCalendar gc = new GregorianCalendar();
			gc.set(Calendar.YEAR,2022);//设置年
			gc.set(Calendar.MONTH,11);//这里0是1月..以此向后推
			gc.set(Calendar.DAY_OF_MONTH,19+i);//设置天
			date = gc.getTime();
			StringDatePart1[i]=Dateformat.format(date);
			System.out.println(StringDatePart1[i]);
		}
		//获取到了这五天的日期
		String[] test= {"已完成 已完成 无","已完成 未完成 忘记核酸","未完成 未完成 忘记打卡"};	
		
		try {
			//teacher表的数据插入操作
			sql_findDepName="Select TeaId,DepName,TeaName From teacher";
			rs=new DBCon().find(sql_findDepName);
			int i=0;
			while(rs.next()) {
				UserNum[i][0]=rs.getString("TeaId"); //教师编号
				UserNum[i][1]=rs.getString("DepName"); //学院名称
				i++;
			}
			//获取到了全部教师账号
			//写循环
			
			for(i=4;i>0;i++) {
				for(int j=0;j<50;j++) {
					//该天打卡的时间
					DateTime RandomDateTime=CR.randomDateTime(StringDatePart1[i]+" 06:00:00",StringDatePart1[i]+" 22:00:00");
					
					String str_datetime=RandomDateTime.toString("yyyy-MM-dd HH:MM:SS");
					StringBuilder sd=new StringBuilder(str_datetime);
					if(Integer.parseInt(str_datetime.substring(17, 19))>=60) {
						//如果秒数大于60
						int last=rd.nextInt(58);
						if(last<10) {
							sd.replace(17,19,"0"+String.valueOf(last));
							str_datetime=sd.toString();
						}else {
							sd.replace(17,19,String.valueOf(last));
							str_datetime=sd.toString();
						}
					}
					
					
					String Num=UserNum[j][0]; //教师编号
					String DepName=UserNum[j][1]; //学院名称
					int randrs=rd.nextInt(3);
					String res=test[randrs]; //获取到随机的结果
					String[] res_split=res.split(" "); //将空格划分结果
					
					sql= "insert into teastatistics values('"+Num+"','"+str_datetime+"','"+DepName+"','"+res_split[0]+"','"+
							res_split[1]+"','"+res_split[2]+"')";
					int ret;
					ret=DC.query(sql);
					System.out.println("ret="+ret);
				}
				
			}
		}catch (ClassNotFoundException | SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
